package com.iaz.tech.tools.generator.java.impl;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iaz.tech.tools.common.utils.UtilTools;
import com.iaz.tech.tools.generator.java.IJavaCodeGeneratorAble;
import com.iaz.tech.tools.property.IPropertyAble;
import com.iaz.tech.tools.property.impl.PropertyCollection;
import com.iaz.tech.tools.property.impl.PropertyGroup;
import com.iaz.tech.tools.property.impl.ValueProperty;
import com.iaz.tech.tools.uml.IUMLNeuronAble;
import com.iaz.tech.tools.uml.relationship.impl.NeuronGeneralizationsRelationship;
import com.iaz.tech.tools.uml.relationship.impl.NeuronRealizationsRelationship;
import com.iaz.tech.tools.uml.structural.classstruct.IClassStructAble; 
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronClassStruct;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronInterfaceStruct;
import com.iaz.tech.tools.uml.structural.pack.IPackageAble; 
/**
 * java 代码生成引擎
 */
public class JavaCodeGenerator implements IJavaCodeGeneratorAble {
	/**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(JavaCodeGenerator.class);
	
	@Override
	public String generatorHeader(IUMLNeuronAble content, IPropertyAble properties) {
		String result = "";
		try {
			if (content == null) {
				return result;
			}
			if (!(content instanceof IClassStructAble)) {
				return result;
			}
			
			if (!(content instanceof IPackageAble)) {
				return result;
			}
			
			String path = ((IPackageAble)content).toPackage();
			if (UtilTools.IsNull(path)) {
				path = "system";
			}
			/* 生成package path */
			result += "package";
			result += " ";
			result += UtilTools.Trim(path);
			result += ";";
			result += "\n\r";
			/*import com.class.name*/
			
			return result;
			
		}catch(Exception e) {
			logger.error("异常：" + e.getMessage());
			result = "";
		}
		return result;
	}
	/**
	 * 此方法生成import 语句
	 */
	@Override
	public String generatorDynamicHeader(IUMLNeuronAble content, IPropertyAble properties) {
		String result = "";
		try {
			if (properties == null) {
				logger.warn("入口参数properties为空，没有输入项目。");
				return result;
			}
			
			if (properties instanceof PropertyGroup) {
				PropertyGroup propertyGroup = (PropertyGroup)properties;
				Map<String, IPropertyAble> children_maps = propertyGroup.getProperties();
				if (children_maps.isEmpty()) {
					return result;
				}
				
				for(Map.Entry<String, IPropertyAble> entry: children_maps.entrySet()) {
					if( UtilTools.IsNull(entry.getKey()) ) {
						continue;
					}
					if(entry.getValue() == null) {
						continue;
					}
					IPropertyAble _step_property = entry.getValue();
					if (!(_step_property instanceof ValueProperty)) {
						continue;
					}
					ValueProperty _value_step_property = (ValueProperty)_step_property;
					if(_value_step_property.getPropDataType() != String.class) {
						continue;
					}
					
					result += "import";
					result += " ";
					result += (String) _value_step_property.getObject();
					result += ";";
					result += "\n\r";
				}
				
				return result;
			}
			
			if (properties instanceof ValueProperty) {
				ValueProperty _value_step_property = (ValueProperty)properties;
				if(_value_step_property.getPropDataType() != String.class) {
					return result;
				}
				
				result += "import";
				result += " ";
				result += (String) _value_step_property.getObject();
				result += ";";
				result += "\n\r";
				return result;
			}
			
			if (properties instanceof PropertyCollection) {
				PropertyCollection property_collection = (PropertyCollection)properties;
				if (property_collection.getCollectionProperty() == null) {
					return result;
				}
				if (property_collection.getCollectionProperty().isEmpty()) {
					return result;
				}
				for(Map.Entry<Long, IPropertyAble> entry : property_collection.getCollectionProperty().entrySet()) {
					if (entry.getValue() == null) {
						continue;
					}
					IPropertyAble propertAble = entry.getValue();
					if (!(propertAble instanceof ValueProperty)) {
						continue;
					}
					ValueProperty value_property = (ValueProperty)propertAble;
					result += "import";
					result += " ";
					result += (String) value_property.getObject();
					result += ";";
					result += "\n\r"; 
				}
				return result;
			}
			
		}catch(Exception e) {
			logger.error("异常：" + e.getMessage());
			result = "";
		}
		return result;
	}

	/**
	 * 创建Class body
	 */
	@Override
	public String generatorClassBody(List<IUMLNeuronAble> contents, IPropertyAble properties) {
		String result = "";
		try {
			if (contents == null) {
				throw new Exception("入口参数content，没有指定，或者为空。 ");
			}
			if (contents.size() == 0) {
				throw new Exception("入口参数content，没有指定，或者为空。 ");
			}
			
			IUMLNeuronAble out_class = null;
			IUMLNeuronAble extends_class = null;
			List<IUMLNeuronAble> implements_class = new ArrayList<IUMLNeuronAble>();			
			
			for( int i = 0; i < contents.size(); i++ ) {
				IUMLNeuronAble uml_neuron = contents.get( i );
				if (uml_neuron == null ) {
					continue;
				}
				
				if (uml_neuron instanceof NeuronClassStruct) {
					out_class = uml_neuron;
					continue;
				}
				if (uml_neuron instanceof NeuronGeneralizationsRelationship) {
					if (extends_class != null) {
						throw new Exception("extends 对象");
					}
					extends_class = uml_neuron;
					continue;
				}
				if (uml_neuron instanceof NeuronRealizationsRelationship) {
					implements_class.add(uml_neuron);
					continue;
				}
			}
			
			NeuronClassStruct neuron_class_struct = (NeuronClassStruct)out_class;
			result += "public class";
			result += UtilTools.Trim(neuron_class_struct.getName());
			
			/* 根据关系，生成Extend, Implements */
			NeuronGeneralizationsRelationship relation_Class = (NeuronGeneralizationsRelationship)extends_class;
			if (relation_Class != null) {
				result += " ";
				result += "extends";
				result += " ";
				result += relation_Class.getCalled();
				result += " ";
			}
			int sel_index = 0;
			String _t_result = "";
			for(int i = 0; i < implements_class.size(); i++) {
				IUMLNeuronAble cursor_able = implements_class.get(i);
				if (cursor_able == null ) {
					continue;
				}
				
				if(sel_index > 0) {
					_t_result = ", ";
				}
				
				NeuronRealizationsRelationship realiza_relation = (NeuronRealizationsRelationship)cursor_able;
				_t_result += realiza_relation.getCalled();
				sel_index++;
			}
			
			if (!UtilTools.IsNull(_t_result)) {
				result += "implements";
				result += _t_result;
			}
			
		}catch(Exception e) {
			logger.error("异常：" + e.getMessage());
			result = "";
		}
		return result;
	}

	@Override
	public String generatorInterfaceBody(List<IUMLNeuronAble> contents, IPropertyAble properties) {
		String result = "";
		try {
			if (contents == null) {
				throw new Exception("入口参数content，没有指定，或者为空。 ");
			}
			if (contents.size() == 0) {
				throw new Exception("入口参数content，没有指定，或者为空。 ");
			}
			
			IUMLNeuronAble out_class = null;
			IUMLNeuronAble extends_class = null;
			
			for( int i = 0; i < contents.size(); i++ ) {
				IUMLNeuronAble uml_neuron = contents.get( i );
				if (uml_neuron == null ) {
					continue;
				}
				
				if (uml_neuron instanceof NeuronInterfaceStruct) {
					out_class = uml_neuron;
					continue;
				}
				if (uml_neuron instanceof NeuronGeneralizationsRelationship) {
					if (extends_class != null) {
						throw new Exception("extends 对象");
					}
					extends_class = uml_neuron;
					continue;
				}
			}
			
			NeuronInterfaceStruct neuron_class_struct = (NeuronInterfaceStruct)out_class;
			result += "public interface";
			result += UtilTools.Trim(neuron_class_struct.getName());
			
			/* 根据关系，生成Extend, Implements */
			NeuronGeneralizationsRelationship relation_Class = (NeuronGeneralizationsRelationship)extends_class;
			if (relation_Class != null) {
				result += " ";
				result += "extends";
				result += " ";
				result += relation_Class.getCalled();
				result += " ";
			}
		}catch(Exception e) {
			logger.error("异常：" + e.getMessage());
			result = "";
		}
		return result;
	}

	/**
	 * 生成Class Body 
	 */
	@Override
	public String generatorBody(IUMLNeuronAble content, IPropertyAble properties) {
		String result = "";
		try {
			
			result += "{";
			result += "\n\r";
			
			result += generatorProperty(content, properties);
			
			result += generatorFunction(content, properties);
			result += "\n\r";
			
			result += "}";
			result += "\n\r";
		}catch(Exception e) {
			logger.error("异常：" + e.getMessage());
			result = "";
		}
		return result;
	}
	
	@Override
	public String generatorProperty(IUMLNeuronAble content, IPropertyAble properties) {
		String result = "";
		try {
			if (!(content instanceof NeuronClassStruct)) {
				return result;				
			}
			
			
			
		}catch(Exception e) {
			logger.error("异常：" + e.getMessage());
			result = "";
		}
		return result;
	}

	@Override
	public String generatorFunction(IUMLNeuronAble content, IPropertyAble properties) {
		String result = "";
		try {
			
			
		}catch(Exception e) {
			logger.error("异常：" + e.getMessage());
			result = "";
		}
		return result;
	}

	

}
